<?php
// +----------------------------------------------------------------------
// Csmmeet [ Csmonline ]
// Author: chensm <chenshiming0802@163.com>
// Create by chensm at 2020-03-31
// +----------------------------------------------------------------------
namespace app\admin\controller\csmonline;

use addons\csmonline\library\CsmBackend;
use addons\csmonline\library\CsmonlineCron;

/**
 * Tables
 * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmonline/chartonlinedate/index
 */
class Chartonlinedate extends CsmBackend
{
    protected $model = null;

    protected $relationSearch = true;

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $begin = $this->csmreq("begin", true);
            $end = $this->csmreq("end", true);

            $today = date("Y-m-d");
            CsmonlineCron::crononedate($today, true);

            $begindate = strtotime($begin);
            $enddate = strtotime($end);
            $calcday = ($enddate - $begindate) / 24 / 3600;
            $ips = [];
            $pvs = [];
            $uvs = [];
            $vvs = [];
            $dau = [];
            for ($i = 0; $i <= $calcday; $i ++) {
                $dd = date("Y-m-d", $begindate);
                $ips[$dd] = [
                    $dd,
                    0,
                    0
                ];
                $pvs[$dd] = [
                    $dd,
                    0,
                    0
                ];
                $uvs[$dd] = [
                    $dd,
                    0,
                    0
                ];
                $vvs[$dd] = [
                    $dd,
                    0,
                    0
                ];
                $dau[$dd] = [
                    $dd,
                    0,
                    0
                ];
                $begindate = $begindate + 3600 * 24;
            }

            $dao = new \app\admin\model\csmonline\Piestatdate();
            $list = $dao->where("statdate", ">=", $begin)
                ->where("statdate", "<=", $end)
                ->order("statdate", "asc")
                ->select();
            foreach ($list as $v) {
                $ips[$v->statdate][1] = $v->ipsn;
                $pvs[$v->statdate][1] = $v->pvsn;
                $uvs[$v->statdate][1] = $v->uvsn;
                $vvs[$v->statdate][1] = $v->vvsn;
                $dau[$v->statdate][1] = 0;

                $ips[$v->statdate][2] = $v->ipsy;
                $pvs[$v->statdate][2] = $v->pvsy;
                $uvs[$v->statdate][2] = $v->uvsy;
                $vvs[$v->statdate][2] = $v->vvsy;
                $dau[$v->statdate][2] = $v->dau;
            }

            $result = array(
                // bootstraptable返回的数据
                "code" => "1",
                "total" => count($list),
                "rows" => $list,
                // Echart通过fastadmin调用数据
                'code' => 1,
                'msg' => "",
                "data" => [
                    "ips" => $this->adjust($ips),
                    "pvs" => $this->adjust($pvs),
                    "uvs" => $this->adjust($uvs),
                    "vvs" => $this->adjust($vvs),
                    "dau" => $this->adjust($dau)
                ]
            );
            return json($result);
        }
        return $this->view->fetch();
    }

    private function adjust($list)
    {
        $ll = [];
        foreach ($list as $v) {
            $ll[] = $v;
        }
        array_unshift($ll, [
            'product',
            '游客统计', '登录用户统计']);
        return $ll;
    }
}
